<?php
namespace web\home\controller;


class Listpage extends Common{
	public function index(){
		//title标签数据
		$conf=['title'=>'莜莜博客—列表页'];
		View::with('conf',$conf);
		//请求地址栏的cid/tid
		$cid = q('get.cid',0,'intval');
		$tid = q('get.tid',0,'intval');
		//如果点击标签
		if ($tid){
			$headData = [
				'type'=>'标签',
				'name'=>Db::table('tag')
					->where('tid',$tid)
					//单一获取tname字段
					->pluck('tname'),
				'total'=>Db::table('article_tag')
					->join('article','article_aid','=','aid')
					->where('tag_tid',$tid)
					->where('is_recycle',1)->count(),
			];
			$articleData = Db::table('article_tag')
				->join('article','article_aid','=','aid')
				->where('tag_tid',$tid)
				->where('is_recycle',1)->get();
			foreach ($articleData as $k=>$v){
				$articleData[$k]['tag']=Db::table('article_tag')
					->join('tag','tag_tid','=','tid')
					->where('article_aid',$v['aid'])
					->field(['tid','tname'])->get();
			}
			
		}
		//如果是点击分类
		if($cid) {
			$categoryModel = new \system\model\Category();
			$cids = $categoryModel->getSon($categoryModel->get(), $cid);
			//把自己追加进去
			$cids[] = $cid;
			$headData = [
				'type' => '分类',
				'name' => Db::table('category')
					->where('cid', $cid)
					//单一获取tname字段
					->pluck('cname'),
				'total' => Db::table('article')
					->whereIn('category_cid', $cids)
					->where('is_recycle', 1)
					->count(),
			];
			$articleData = Db::table('article')
				->join('category', 'category.cid', '=', 'article.category_cid')
				->where('is_recycle', 1)
				->whereIn('category_cid', $cids)
				->get();
			foreach ($articleData as $k => $v) {
				$articleData[$k]['tag'] = Db::table('article_tag')
					->join('tag', 'tag_tid', '=', 'tid')
					->where('article_aid', $v['aid'])
					->field(['tid', 'tname'])->get();

			}
		}
		View::with('headData',$headData);
		View::with('articleData',$articleData);
		View::make();

	}
}
